<template>
  <div class="panel">
    <el-dialog
      title="新增员工"
      :visible="dialogShow"
      @close="pancel"
      :close-on-click-modal="false"
    >
      <el-form :model="form" label-width="90px" :rules="rules" ref="myform">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker
            v-model="form.timeOfEntry"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select v-model="form.formOfEmployment" placeholder="请选择">
            <el-option
              v-for="(item, index) in hireType"
              :key="index"
              :label="item.value"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="form.workNumber"></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <div class="box">
            <!-- 输入框 -->
            <el-input @focus="myfocus" v-model="form.departmentName"></el-input>
            <!-- 树状结构 -->
            <el-tree
              :props="{ label: 'name' }"
              v-if="showTree"
              :data="list"
              class="mytree"
              default-expand-all
              :expand-on-click-node="false"
              @node-click="nodeclick"
            ></el-tree>
          </div>
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker
            v-model="form.correctionTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="pancel">取 消</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//导入枚举数据
import employ from '@/api/constant/employees'
//导入获取组织架构数据的方法
import { getDepartmentsApi } from '@/api/departments'
//导入添加员工方法
import { addEmplList } from '@/api/employees'
export default {
  //新增面板、编辑面板的数据源，由父组件传过来
  props: ['dialogShow'],
  data () {
    return {
      //选择器的数据源
      hireType: employ.hireType,
      //表单的数据源
      form: {
        username: '', //员工姓名
        mobile: '', //员工手机号
        formOfEmployment: '', //聘用形式
        workNumber: '', //工号
        departmentName: '', //部门名称
        timeOfEntry: '', //入职时间
        correctionTime: '' //转正时间
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          { min: 11, max: 11, message: '长度为11个字符哦', trigger: 'blur' },
          {
            validator: (rules, value, callback) => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              reg.test(value)
                ? callback()
                : callback(new Error('手机号格式错误'))
            }
          }
        ],
        formOfEmployment: [
          { required: true, message: '姓名不能为空', trigger: 'change' }
        ],
        workNumber: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '姓名不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ]
      },
      //部门结构中的树状结构的数据源
      list: [],
      // 控制树状结构是否显示
      showTree: false
    }
  },
  methods: {
    //点击确定按钮
    submit () {
      //进行全局验证
      this.$refs.myform.validate(async val => {
        if (val) {
          //验证通过时
          const res = await addEmplList(this.form)
          //关闭面板
          this.$emit('update:dialogShow', false)
          //清空数据
          this.form = {
            username: '', //员工姓名
            mobile: '', //员工手机号
            formOfEmployment: '', //聘用形式
            workNumber: '', //工号
            departmentName: '', //部门名称
            timeOfEntry: '', //入职时间
            correctionTime: '' //转正时间
          }
          //重置校验规则
          this.$refs.myform.resetFields()
          //提示新增成功
          this.$message.success('新增员工成功')
          //重新加载数据，渲染页面
          this.$emit('updata')
        }
      })
    },

    //获取组织架构中的数据
    async getDepartments () {
      const res = await getDepartmentsApi()
      //绑定数据到部门数据中
      // console.log(res);
      this.list = this.turnData(res.data.depts, '')
    },
    //讲并列结构转换成树状结构
    turnData (arr, pid) {
      const newArr = []
      arr.forEach(item => {
        if (item.pid === pid) {
          const children = this.turnData(arr, item.id)
          if (children.length > 0) {
            item.children = children
          }
          newArr.push(item)
        }
      })
      return newArr
    },
    //点击输入框获取焦点事件后，显示树状结构
    myfocus () {
      this.showTree = true
    },
    //点击树状结构中的数据，关闭下拉树状结构
    nodeclick (data) {
      //关闭树状结构
      this.showTree = false
      //点击树状结构中的数据，渲染到输入框中
      this.form.departmentName = data.name
    },
    //点击面板的取消按钮触：关闭面板
    pancel () {
      this.$emit('update:dialogShow', false)
    }
  },
  created () {
    //得到部门数据
    //调用获取组织架构的方法
    this.getDepartments()
  }
}
</script>

<style lang="scss">
.panel {
  .box {
    position: relative;
    width: 300px;
    .mytree {
      position: absolute;
      border: 1px solid #ddd;
      border-radius: 5px;
      width: 100%;
      top: 44px;
      left: 0;
      z-index: 999;
      .el-tree-node__content {
        height: 30px;
        .el-icon-caret-right {
          display: none;
        }
        .el-tree-node__label {
          margin-left: 10px;
        }
      }
    }
  }
}
</style>
